package cdu.xjj.repository.service;

import cdu.xjj.pojo.entity.service.TestItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface TestItemRepository extends JpaRepository<TestItem, Long> {

    /**
     * 根据学生id查询对应的体测条目
     * @param studentId 学生id
     */
    @Query("SELECT ti FROM TestItem ti " +
            "JOIN ItemGrade ig ON ti.id = ig.itemId " +
            "JOIN Grade g ON ig.gradeId = g.id " +
            "JOIN Clazz c ON c.gradeId = g.id " +
            "JOIN Student s ON s.classId = c.id " +
            "WHERE s.id = :studentId " +
            "AND (ti.genderLimit = 2 " + // 通用项目
            "    OR (ti.genderLimit = 0 AND s.gender = 1) " + // 仅限男生
            "    OR (ti.genderLimit = 1 AND s.gender = 2))") // 仅限女生
    List<TestItem> findTestItemsByStudentId(@Param("studentId") Long studentId);



}